package cn.kgc.controller;

import cn.kgc.pojo.Train;
import cn.kgc.service.TrainService;
import cn.kgc.util.UploadUntils;
import cn.kgc.util.UploadUntilsJ;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.List;

/**
 * @author: fighting
 * @date: 2022/2/16
 * @description:
 */
@Controller
public class TrainController {

    @Resource
    private TrainService trainService;


    /**
     * 文件下载
     */
    @GetMapping("train/download")
    public void download(String openStyle, @RequestParam("trainId") Integer trainId, HttpServletResponse response) throws IOException {
        openStyle = openStyle == null ? "attachment" : openStyle;
        // 根据文件id获取对应的文件信息
        Train train = trainService.selById(trainId);

        // 获取文件路径
        System.out.println("File: " + train);
        String property = System.getProperty("user.dir") + "\\src\\main\\resources\\upload\\" + train.getData();
        System.out.println(property);
        // 获取文件输入流（把磁盘上的文件通过IO加载到程序（内存）中
        FileInputStream fis = new FileInputStream(new File(property));
        // 附件下载
        // 注意：为防止中文乱码，需要使用UTF-8
        response.setHeader("content-disposition", openStyle + ";fileName=" + URLEncoder.encode(train.getData(), "UTF-8"));
        // 获取响应流（找到后需要通过Response发送回给用户
        ServletOutputStream os = response.getOutputStream();
        // 文件拷贝
        IOUtils.copy(fis, os);
        // 关闭流
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(os);
    }


    //反馈保存
    @RequestMapping("train/fankuibaocun")
    @ResponseBody
    public Boolean fankuibaocun(@ModelAttribute Train train) {
        boolean bo = trainService.FKupd(train);
        System.out.println("返回结果为" + bo);

        return bo;
    }

    //保存=》起草
    @RequestMapping("/train/updbaocun")
    @ResponseBody
    public boolean updbaocun(@ModelAttribute Train train,
                             @RequestParam("startTimes") String startTimes,
                             @RequestParam("endTimes") String endTimes) {
        train.setStartTime(Date.valueOf(startTimes));
        train.setEndTime(Date.valueOf(endTimes));
        System.out.println(train.getEndTime());
        System.out.println("传参：" + train.toString());
        boolean bo = trainService.upd(train);
        return bo;
    }


    @RequestMapping("/培训反馈")
    public String peixunfankui(Model model,
                               Integer trainId) {
        System.out.println(trainId);
        Train train = trainService.selById(trainId);
        java.util.Date startTime = train.getStartTime();
        java.util.Date end = train.getEndTime();
        Date date1 = new Date(startTime.getTime());
        Date date2 = new Date(end.getTime());
        train.setStartTime(date1);
        train.setEndTime(date2);
        System.out.println(train.toString());
        model.addAttribute("train", train);
        return "培训反馈";
    }


    @RequestMapping("/培训计划")
    public String peixunjihua(Model model) {

        return "培训计划";
    }


    //驳回
    @RequestMapping("train/bohui")
    @ResponseBody
    public Boolean bohui(@ModelAttribute Train train) {
        boolean bo = trainService.fuhe(train);
        System.out.println("返回结果为" + bo);

        return bo;
    }

    @RequestMapping("train/tongguo")
    @ResponseBody
    public Boolean tongguo(@ModelAttribute Train train) {
        boolean bo = trainService.fuhe2(train);
        System.out.println("返回结果为" + bo);

        return bo;
    }


    @RequestMapping("/培训复核")
    public String peixunfuhe(Model model,
                             Integer trainId) {
        System.out.println(trainId);
        Train train = trainService.selById(trainId);
        java.util.Date startTime = train.getStartTime();
        java.util.Date end = train.getEndTime();
        Date date1 = new Date(startTime.getTime());
        Date date2 = new Date(end.getTime());
        train.setStartTime(date1);
        train.setEndTime(date2);
        System.out.println(train.toString());
        model.addAttribute("train", train);
        return "培训复核";
    }

    //复核 查询单个
    @RequestMapping("train/fuhechaxundange")
    @ResponseBody
    public String fuhechaxundange(Model model, @RequestParam("trianId") Integer trainId) {
        Train train = trainService.selById(trainId);
        model.addAttribute("train", train);
        return JSON.toJSONStringWithDateFormat(train, "yyyy-MM-dd");
    }


    @RequestMapping("/train/sel")
    @ResponseBody
    public String sel() {
        List<Train> list = trainService.sel();
        return JSON.toJSONStringWithDateFormat(list, "yyyy-MM-dd");
    }

    @RequestMapping("/train/trainMoFenSel")
    @ResponseBody
    public String trainMoFenSel(@RequestParam("name") String name,
                                @RequestParam("status") String status,
                                @RequestParam("startTime") String startTime,
                                @RequestParam("currentPage") Integer currentPage,
                                @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Train> list = trainService.MoHuFenYe(name, status, startTime);
        for (Train train : list
        ) {
            System.out.println("id为" + train.getTrainId());
        }
        PageInfo<Train> pageInfo = new PageInfo<Train>(list);

        return JSON.toJSONStringWithDateFormat(pageInfo, "yyyy-MM-dd");
    }

        @RequestMapping("/培训修改")
    public String upd(Model model,
                      Integer trainId) {
        System.out.println(trainId);
        Train train = trainService.selById(trainId);
        java.util.Date startTime = train.getStartTime();
        java.util.Date end = train.getEndTime();
        Date date1 = new Date(startTime.getTime());
        Date date2 = new Date(end.getTime());
        train.setStartTime(date1);
        train.setEndTime(date2);
        System.out.println(train.toString());
        model.addAttribute("train", train);
        return "培训修改";
    }
//    @RequestMapping("/培训修改")
//    public String upd(Model model,
//                      Integer trainId,
//                      @RequestParam("file") MultipartFile file) {
//
//
//        System.out.println(trainId);
//        Train train = trainService.selById(trainId);；
//        java.util.Date startTime = train.getStartTime();
//        java.util.Date end = train.getEndTime();
//        Date date1 = new Date(startTime.getTime());
//        Date date2 = new Date(end.getTime());
//        train.setStartTime(date1);
//        train.setEndTime(date2);
//
//        String upload = UploadUntils.upload(file);//获得了一个文件路径
//        System.out.println(upload);
//        train.setData(upload);
//
//        System.out.println(train.toString());
//        model.addAttribute("train", train);
//        return "培训修改";
//    }


    //保存=》起草
    @RequestMapping("/train/add1")
    @ResponseBody
    public String add1(@ModelAttribute Train train,
                       @RequestParam("startTime") String startTime,
                       @RequestParam("endTime") String endTime,
                       @RequestParam("file") MultipartFile file) {
        //首先调用我们写的工具类

        String upload = UploadUntils.upload(file);//获得了一个文件路径
        System.out.println(upload);
        train.setData(upload);

        train.setStartTime(Date.valueOf(startTime));
        train.setEndTime(Date.valueOf(endTime));


        boolean bo = trainService.add(train);
        return JSON.toJSONString(bo);
    }

    @RequestMapping("/train/add2")
    @ResponseBody
    public String add2(@ModelAttribute Train train,
                       @RequestParam("startTime") String startTime,
                       @RequestParam("endTime") String endTime,
                       @RequestParam("file") MultipartFile file) {
        //首先调用我们写的工具类

        String upload = UploadUntils.upload(file);//获得了一个文件路径
        System.out.println(upload);
        train.setData(upload);

        train.setStartTime(Date.valueOf(startTime));
        train.setEndTime(Date.valueOf(endTime));


        boolean bo = trainService.add2(train);
        return JSON.toJSONString(bo);
    }

//    //申请复核
//    @RequestMapping("/train/add2")
//    @ResponseBody
//    public Boolean add2(@ModelAttribute Train train,
//                        @RequestParam("startTimes") String startTimes,
//                        @RequestParam("endTimes") String endTimes) {
//        train.setStartTime(Date.valueOf(startTimes));
//        train.setEndTime(Date.valueOf(endTimes));
//        System.out.println(train.getEndTime());
//        System.out.println("传参：" + train.toString());
//        boolean bo = trainService.add2(train);
//        return bo;
//    }


    @RequestMapping("/新增培训")
    public String xinzengpeixun() {
        return "新增培训";
    }


}
